import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/home',
        redirect: "/home/echarts"
      },
      // 用户列表
      {
        path: "users",
        component: () => import(/* webpackChunkName: "users" */ '../views/home/Users.vue'),
        meta: {
          bread: ["用户管理", "用户列表"]
        }
      },
      // 权限管理
      {
        path: "roles",
        component: () => import(/* webpackChunkName: "roles" */ '../views/home/Roles.vue'),
        meta: {
          bread: ["权限管理", "角色列表"]
        }
      },
      {
        path: "rights",
        component: () => import(/* webpackChunkName: "rights" */ '../views/home/Rights.vue'),
        meta: {
          bread: ["权限管理", "权限列表"]
        }
      },
      {
        path: "goods",
        component: () => import(/* webpackChunkName: "goods" */ '../views/goods/GoodsList.vue'),
        meta: {
          bread: ["商品管理", "商品列表"]
        }
      },
      {
        path: "echarts",
        component: () => import(/* webpackChunkName: "echarts" */ '../views/home/Echarts.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    next()
  } else {
    if (sessionStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
